/**
 * 获取教师列表
 * 
 * /admin/teacher/getlist
 */

const User_info = require('../../../models/usersInfo')
const utils = require('../../../utils/index')
const Sequelize = require('sequelize')
const Op = Sequelize.Op
const $ = require('../../../utils/common')

const get_teacher_list = async function (req, res) {
    try {
        // 学生表和教师表合并在一起了
        var query = {}
        if (req.query.user_name) { query['user_name'] = req.query.user_name }
        if (req.query.user_fullname) {query['user_fullname'] = req.query.user_fullname}
        if (req.query.user_phone) {query['user_phone'] = req.query.user_phone}
        var offset = 1
        var limit = parseInt(req.query.size)

        if (req.query.page > 1) {
            offset = parseInt(req.query.size) * (parseInt(req.query.page) - 1)
        } else if (req.query.page == 1) {
            offset = 0
        }

        // 模糊搜索
        const sql_res = await User_info.findAndCountAll({
            offset: offset,
            limit: limit,
            order: [
                ['user_name', 'DESC'],
            ],
            where: {
                user_role: {
                    [Op.or]: ['jiaoyan_teacher', 'xingzheng_teacher']
                },
                user_name: {
                    [Op.like]: '%' + (query.user_name === undefined ? '' : query.user_name) + '%'
                },
                user_fullname: {
                    [Op.like]: '%' + (query.user_fullname === undefined ? '' : query.user_fullname) + '%'
                }
            },
            attributes: ['id', 'user_name', 'user_fullname', 'user_address', 'office_phone',  'user_phone', 'user_status', 'user_role'],
        })
        
        res.json({
            code: 200,
            message: 'success',
            data: {
                list: sql_res.rows,
                total: sql_res.count
            }
        })

        // 日志
        utils.create_a_log(req, '教师列表', '浏览')
        return
    }
    catch(error) {
        $.sendMessage(res, 0000, 'error' + error)
        return
    }
}
    
module.exports = get_teacher_list